package com.egg.ylt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.adapter.ADA_CancelList;
import com.egg.ylt.pojo.AppointmentServeListEntity;
import com.egg.ylt.pojo.CancelAppoinEntity;
import com.egg.ylt.presenter.impl.CancelAppoinPresenterImpl;
import com.egg.ylt.view.ICancelAppointView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_CancelAppointment extends BaseActivity<CancelAppoinPresenterImpl> implements ICancelAppointView {

    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.include_tv_right)
    TextView includeTvRight;
    @BindView(R.id.include_rl_view)
    RelativeLayout includeRlView;
    @BindView(R.id.recycler_cancel)
    RecyclerView recyclerCancel;
    @BindView(R.id.edit_cause)
    EditText editCause;
    @BindView(R.id.btn_cancel_appoin)
    TextView btnCancelAppoin;

    private List<CancelAppoinEntity> mCancelList;
    private ADA_CancelList mCancelAdapter;
    private AppSharedPreferences preferences;
    private String mToken;

    private String mId;
    private String mOrderCode;

    @Override
    protected void getBundleExtras(Bundle extras) {
        mId = extras.getString("id");
        mOrderCode = extras.getString("orderCode");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_cancel_appointment;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        //动态设置沉浸式状态栏的高度
        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, includeRlView);

        //设置title
        includeTvTitle.setText("取消预约");

        //获取token和订单数据
        preferences = new AppSharedPreferences(mContext);
        mToken = preferences.getString(Constants.USER_TOKEN);

        //设置取消原因数据
        initData();
        //初始化recycle
        initRecycler();

    }

    private void initData() {
        mCancelList = new ArrayList<>();

        CancelAppoinEntity cancelEntity1 = new CancelAppoinEntity();
        cancelEntity1.setSure(false);
        cancelEntity1.setName("选择时间有冲突，重新预约");
        CancelAppoinEntity cancelEntity2 = new CancelAppoinEntity();
        cancelEntity2.setSure(false);
        cancelEntity2.setName("服务不好，我不想去了");
        CancelAppoinEntity cancelEntity3 = new CancelAppoinEntity();
        cancelEntity3.setSure(false);
        cancelEntity3.setName("联系不上商家");
        CancelAppoinEntity cancelEntity4 = new CancelAppoinEntity();
        cancelEntity4.setSure(false);
        cancelEntity4.setName("其它");

        mCancelList.add(cancelEntity1);
        mCancelList.add(cancelEntity2);
        mCancelList.add(cancelEntity3);
        mCancelList.add(cancelEntity4);

    }

    private void initRecycler() {
        //设置布局管理器
        recyclerCancel.setLayoutManager(new LinearLayoutManager(mContext));
        //设置适配器
        mCancelAdapter = new ADA_CancelList(mContext);
        recyclerCancel.setAdapter(mCancelAdapter);

        mCancelAdapter.setOnItemClickListener(new ADA_CancelList.OnItemClickListener() {
            @Override
            public void onClick(String text) {
                if (text.equals("其它")) {
                    editCause.setVisibility(View.VISIBLE);
                    editCause.setFocusableInTouchMode(true);

                    editCause.setFocusable(true);

                    editCause.requestFocus();
                } else {
                    editCause.setVisibility(View.INVISIBLE);
                    editCause.setFocusableInTouchMode(false);

                    editCause.setFocusable(false);

                    editCause.requestFocus();
                }
            }
        });
        //设置数据
        mCancelAdapter.addList(mCancelList);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void setCancelSucceed() {
        //取消预约成功
        ToastUtil.makeText(mContext, "取消成功", false);
        Intent intent = new Intent();
        intent.putExtra("orderStatus", 3);
        setResult(1, intent);
        finish();
    }

    @OnClick({R.id.include_iv_back, R.id.btn_cancel_appoin, R.id.edit_cause, R.id.btn_not_appoin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.btn_cancel_appoin://确认取消预约
                if (mCancelAdapter.isSelected == -15) {
                    ToastUtil.makeText(mContext, "请选择取消原因", false);
                    return;
                }
                setCancelAppoin();
                break;
            case R.id.edit_cause:
                break;
            case R.id.btn_not_appoin://暂不取消
                finish();
                break;
        }
    }

    private void setCancelAppoin() {
        String cause = editCause.getText().toString().trim();
        String text = null;
        List<CancelAppoinEntity> listData = mCancelAdapter.getListData();
        for (int i = 0; i < listData.size(); i++) {
            CancelAppoinEntity entity = listData.get(i);
            if (entity.isSure()) {
                text = entity.getName();
            }
        }
        if ("其它".equals(text)) {
            if (StringUtil.isEmpty(cause)) {
                ToastUtil.makeText(mContext, "请输入其它原因", false);
                return;
            }
        }
        mPresenter.setCancelAppoin(mToken, mId, "3", cause, mOrderCode);
    }
}
